using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DealerWorld.Forms
{
    public partial class search_ro : Form
    {
        public search_ro()
        {
            InitializeComponent();

            Main.allOrders.ForEach(delegate(RepairOrder ro) { add_lsv_row(ro); });
        }

        private void add_lsv_row(RepairOrder ro)
        {
            string[] row_info = {
                ro.c.name
                ,ro.c.mainPhone
                ,ro.v.licensePlate
                ,ro.v.vin
            };
            ListViewItem lsv_item = new ListViewItem(row_info);
            lsv_item.Tag = ro;
            lsv_ro.Items.Add(lsv_item);
        }

        private bool matchSearch(RepairOrder ro)
        {
            if (txb_cust_name.Text != "")
                return ro.c.name.ToLower().Contains(txb_cust_name.Text.ToLower());
            if (txb_cust_phone.Text != "")
                return ro.c.mainPhone.Contains(txb_cust_phone.Text);
            if (txb_license.Text != "")
                return ro.v.licensePlate.ToLower().Contains(txb_license.Text.ToLower());
            if (txb_ro_number.Text != "")
                return ro.unitNo.ToLower().Contains(txb_ro_number.Text.ToLower());
            if (txb_tag_number.Text != "")
                return ro.tagNo.ToLower().Contains(txb_tag_number.Text.ToLower());
            else return false;
        }

        private void btn_search_Click(object sender, EventArgs e)
        {
            lsv_ro.Items.Clear();

            List<RepairOrder> matchOrders = Main.allOrders.FindAll(matchSearch);

            matchOrders.ForEach(delegate(RepairOrder ro) { add_lsv_row(ro); });
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void key_pressed(Object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_search_Click(sender, e);
            }
        }

        private void search_ro_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_ro_number_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_tag_number_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_cust_name_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_cust_phone_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void txb_license_KeyUp(object sender, KeyEventArgs e)
        {
            key_pressed(sender, e);
        }

        private void lsv_ro_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lsv_ro_DoubleClick(sender, e);
            }
        }

        private void lsv_ro_DoubleClick(object sender, EventArgs e)
        {
            if (lsv_ro.SelectedItems.Count == 1)
            {
                RepairOrder ro = (RepairOrder)lsv_ro.Items[lsv_ro.SelectedIndices[0]].Tag;

                new Forms.repair_order(ro).Show();
            }
        }

    }
}